vimfandomcom-20200223-history
Easier buffer switching
One of the keys to effective Vim usage is effective buffer management. Vim doesn't force you to click on a tab every time you want to look at another file, but if you don't know how to easily find the buffer you want, it can be just as frustrating. Terminology Some of the terms used in this tip are briefly explained below. For more details see . *When a file is loaded into memory for editing, a new buffer is created to hold it. *A buffer can be created to hold text that isn't related to a file, such as a directory listing. *A window is a viewport onto a buffer. *A buffer is active if it is shown in a window. *A buffer is hidden if it is not shown in a window. *A buffer is a listed buffer if it is always shown in the buffer list. *A buffer is an unlisted buffer if it is not shown in the buffer list by default. *Each buffer is given a unique number when it is first created. It keeps this number for the duration of the vim session. *The name of a buffer is the name of the file that has been loaded into it, or for buffers not containing the contents of a file, it can be any string. Listing buffers The :buffers command will list your current buffers. There are two alternate names for this command :ls and :files. By default, only listed buffers will be displayed. Unlisted buffers can be included by prepending the command with a !, such as :buffers!. Switching to another buffer The command to switch to another buffer is :buffer, which is often abbreviated as :b, :bu or :buf. :buffer can be given either the name or the number of the buffer to edit. Switching by name When using the buffer name as the argument to :buffer, you don't have to specify the entire name. However, if more than one buffer matches the given argument then the buffers wont be switched. Any fragment of the buffer name can be used to match against, although buffers which match at the begining of their name will be selected in preference to buffers which match elsewhere in their name. Eg, if there are buffers request_manager.java and queue_manager.java then :buffer que matches both of them, but will switch to queue_manager.java as it matches at the begining. Tab completion can be used to complete the buffer name, :buffer will complete to the full buffer name if only one buffer matches ''''. If more than one buffer matches the matches will be cycled through on further presses of . The switch will take place once is entered with a unique buffer name. Again, any fragment of the buffer name can be used to complete to the matching buffer, (i.e. request_manager.java can be completed using "tma" or "req" or "r.java"). If you would prefer to be able to select the buffer from the list of partial matches the following function can be used. It will jump to the matching buffer if only one match is found, or if there are many matches it will print a list of the matching buffers in the command-line area, and allow you to select one of the matching buffers by buffer number. function! BufSel(pattern) let bufcount = bufnr("$") let currbufnr = 1 let nummatches = 0 let firstmatchingbufnr = 0 while currbufnr <= bufcount if(bufexists(currbufnr)) let currbufname = bufname(currbufnr) if(match(currbufname, a:pattern) > -1) echo currbufnr . ": ". bufname(currbufnr) let nummatches += 1 let firstmatchingbufnr = currbufnr endif endif let currbufnr = currbufnr + 1 endwhile if(nummatches 1) execute ":buffer ". firstmatchingbufnr elseif(nummatches > 1) let desiredbufnr = input("Enter buffer number: ") if(strlen(desiredbufnr) != 0) execute ":buffer ". desiredbufnr endif else echo "No matching buffers" endif endfunction "Bind the BufSel() function to a user-command command! -nargs=1 Bs :call BufSel("") Switching by number If you know the number of the buffer you want to switch to, you can pass that as the argument to :buffer, eg. :buffer 5 will switch to buffer number 5. An alternative which can be used from normal mode is {buffer number}, eg. 5 will switch to buffer number 5. When there are several buffers open in a vim session, it can become difficult to keep track of the buffers and their respective buffer numbers. If this is the case, switching to a different file can be made easier using a simple map: :nnoremap :buffers:buffer When F5 is pressed, a numbered list of file names is printed, and the user needs to type a single number based on the "menu" and press enter. The "menu" disappears after choosing the number so it appears only when you need it. The at the end of the map isn't required, but does help to separate the input from the "prompt". As the :buffer command is being used to perform the switch, the buffer name could be used instead, if prefered. Switching to the previously edited buffer Often times the buffer that you want to edit is the buffer that you have just left. Vim provides a couple of convenient commands to switch back to the previous buffer. These are and :b#. Both of these technically edit the alternate file, although this is usualy the previously edited buffer. Scripts If the above tips don't provide what you require, there are, of course, several scripts covering buffer switching. A few of them are linked to below. * * * * * *Check above links *Include tbufferselect *Search vim scripts for others??? References * * * * * * Comments " Use zl to list buffers, and go to matching buffer. " Type part of bufname after / prompt. :nmap zl :ls!:buf / ---- *Better section titles. *Expand current sections. *Merge in other tips. *What does the / in the above comment do? *Include (taken from comment in tip 412) ** :wn " write current file goto next ** :wp " write current file goto previous ** :last " goto last ** :first " goto first (:rew) *Include (from another comment in tip 412) **" ",1", ",2", .. ",9", ",0" to go straight to that numbered buffer (0 = 10) **" ",g" to toggle between two buffers (my most used probably) **map ,s :bN " back on the buffer list **map ,f :bn " forth on the buffer list **map ,d :buffers " a list of what's in each buffer **map ,g :e# " toggle between two buffers **map ,1 :1b " go to buffer 1 **map ,2 :2b **map ,3 :3b **map ,4 :4b **map ,5 :5b **map ,6 :6b **map ,7 :7b **map ,8 :8b **map ,9 :9b **map ,0 :10b *Include section about 'hidden' and :buffer!